package cn.iocoder.yudao.module.system.service.book;

import cn.iocoder.yudao.module.system.controller.admin.book.vo.BookPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.book.vo.BookSaveReqVO;

import cn.iocoder.yudao.module.system.dal.dataobject.book.BookDO;
import cn.iocoder.yudao.module.system.dal.mysql.book.BookMapper;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;



import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.BOOK_NOT_EXISTS;


/**
 * 绘本 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BookServiceImpl implements BookService {

    @Resource
    private BookMapper bookMapper;
    
    @Resource
    private FileService fileService;

    @Override
    public Long createBook(BookSaveReqVO createReqVO) {
        // 插入
        BookDO book = BeanUtils.toBean(createReqVO, BookDO.class);
        bookMapper.insert(book);
        // 返回
        return book.getId();
    }

    @Override
    public void updateBook(BookSaveReqVO updateReqVO) {
        // 校验存在
        validateBookExists(updateReqVO.getId());
        // 更新
        BookDO updateObj = BeanUtils.toBean(updateReqVO, BookDO.class);
        bookMapper.updateById(updateObj);
    }

    @Override
    public void deleteBook(Long id) {
        // 校验存在
        validateBookExists(id);
        // 删除
        bookMapper.deleteById(id);
    }

    private void validateBookExists(Long id) {
        if (bookMapper.selectById(id) == null) {
            throw exception(BOOK_NOT_EXISTS);
        }
    }

    @Override
    public BookDO getBook(Long id) {
        return bookMapper.selectById(id);
    }

    @Override
    public PageResult<BookDO> getBookPage(BookPageReqVO pageReqVO) {
        return bookMapper.selectPage(pageReqVO);
    }
    
    /**
     * 上传绘本文件
     *
     * @param file 文件
     * @param bookId 绘本ID
     * @return 文件URL
     */
    public String uploadBookFile(MultipartFile file, Long bookId) throws IOException {
        // 使用基础设施模块的文件服务上传文件
        String path = "books/" + bookId + "/" + file.getOriginalFilename();
        String url = fileService.createFile(file.getOriginalFilename(), path, 
                                         file.getBytes());
        
        // 更新绘本的文件URL
        BookDO book = new BookDO();
        book.setId(bookId);
        bookMapper.updateById(book);
        
        return url;
    }
    
    /**
     * 上传绘本封面
     *
     * @param file 封面文件
     * @param bookId 绘本ID
     * @return 封面URL
     */
    public String uploadBookCover(MultipartFile file, Long bookId) throws IOException {
        // 使用基础设施模块的文件服务上传封面
        String path = "books/" + bookId + "/cover/" + file.getOriginalFilename();
        String url = fileService.createFile(file.getOriginalFilename(), path, 
                                         file.getBytes());
        
        // 更新绘本的封面URL
        BookDO book = new BookDO();
        book.setId(bookId);
        book.setCoverUrl(url);
        bookMapper.updateById(book);
        
        return url;
    }

}